package com.neuedu.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.neuedu.entity.UmsDoctor;
import com.neuedu.entity.UmsUser;

import java.util.Date;

public class JwtUtil {
    private static final String KEY = "wangyu";

    public static String createToken(UmsDoctor doctor) {
        return JWT.create()
                .withClaim("id", doctor.getId())
                .withClaim("phone", doctor.getPhone())
                .withClaim("email", doctor.getEmail())
                .withClaim("date", new Date())
                .sign(Algorithm.HMAC256(KEY));
    }
    public static UmsDoctor check(String token) {
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(KEY)).build().verify(token);
        Long id = verify.getClaim("id").asLong();
        String phone = verify.getClaim("phone").asString();
        String email = verify.getClaim("email").asString();
        UmsDoctor doctor = new UmsDoctor();
        doctor.setId(id);
        doctor.setPhone(phone);
        doctor.setEmail(email);
        return doctor;
    }
}
